/***********************************************************************************************************************
    @file    main.c
    @author  FAE Team
    @date    18-Apr-2023
    @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  **********************************************************************************************************************
    @attention

    <h2><center>&copy; Copyright(c) <2023> <MindMotion></center></h2>

      Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
    following conditions are met:
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
       the following disclaimer in the documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
       promote products derived from this software without specific prior written permission.

      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *********************************************************************************************************************/


/* Files include */
#include "platform.h"
#include "readvol.h"


/* Private typedef ****************************************************************************************************/

/* Private define *****************************************************************************************************/
#define avg_num 30
#define ADC_CURRENT_PIN_CH       ADC_Channel_2
/* Private macro ******************************************************************************************************/

/* Private variables **************************************************************************************************/
static uint16_t Get_ADC_Buff[avg_num];
/* Private functions **************************************************************************************************/
/***********************************************************************************************************************
  * @brief
  * @note   none
  * @param  none
  * @retval none
  *********************************************************************************************************************/
void ADC_Configure(void)
{
    ADC_InitTypeDef  ADC_InitStruct;
    GPIO_InitTypeDef GPIO_InitStruct;

    RCC_APB1PeriphClockCmd(RCC_APB1ENR_ADC1, ENABLE);

    ADC_StructInit(&ADC_InitStruct);
    ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStruct.ADC_PRESCARE   = ADC_PCLK1_PRESCARE_16;
    ADC_InitStruct.ADC_Mode       = ADC_Mode_Scan;
    ADC_InitStruct.ADC_DataAlign  = ADC_DataAlign_Right;
    ADC_Init(ADC1, &ADC_InitStruct);

    ADC_SampleTimeConfig(ADC1, ADC_Samctl_240_5);

    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);

    /* PA3(RV2)  */
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_3;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AIN;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    ADC_Cmd(ADC1, ENABLE);
}

/***********************************************************************************************************************
  * @brief
  * @note   none
  * @param  none
  * @retval none
  *********************************************************************************************************************/
float ADC_GetChannelVoltage(ADCCHANNEL_TypeDef Channel)
{
    uint16_t Value = 0;
    float Voltage;

    ADC_ANY_NUM_Config(ADC1, 0);
    ADC_ANY_CH_Config(ADC1, 0, Channel);
    ADC_ANY_Cmd(ADC1, ENABLE);

    ADC_SoftwareStartConvCmd(ADC1, ENABLE);

    while (RESET == ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
    {
    }

    ADC_ClearFlag(ADC1, ADC_FLAG_EOC);

    Value = ADC_GetConversionValue(ADC1);

    ADC_ANY_Cmd(ADC1, DISABLE);

    Voltage = (float)Value / (float)4096 * (float)3.3;

    return (Voltage);
}
uint16_t ADC_GetChannelValue(ADCCHANNEL_TypeDef Channel)
{
    uint16_t Value = 0;

    ADC_ANY_NUM_Config(ADC1, 0);
    ADC_ANY_CH_Config(ADC1, 0, Channel);
    ADC_ANY_Cmd(ADC1, ENABLE);

    ADC_SoftwareStartConvCmd(ADC1, ENABLE);

    while (RESET == ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
    {
    }

    ADC_ClearFlag(ADC1, ADC_FLAG_EOC);

    Value = ADC_GetConversionValue(ADC1);

    ADC_ANY_Cmd(ADC1, DISABLE);

    return (Value);
}

void SortAscending(uint16_t *Pst_Source, uint8_t CNT) {
  uint16_t buff = 0;
  uint8_t i, j;
  for (i = 0; i < (CNT - 1); i++) {
    for (j = i + 1; j < CNT; j++) {
      if (Pst_Source[i] > Pst_Source[j]) {  // ���ݽ���
        buff = Pst_Source[j];
        Pst_Source[j] = Pst_Source[i];
        Pst_Source[i] = buff;
      }
    }
  }
}

float get_vol(void)
{
	uint16_t null_num = 0;
	uint32_t vol_sum = 0;
	uint16_t vol_res = 0;
	for (uint8_t i = 0; i < avg_num; i++) {
    Get_ADC_Buff[i] = ADC_GetChannelValue(ADC_CURRENT_PIN_CH);
		PLATFORM_DelayMS(1);
  }

  SortAscending(Get_ADC_Buff, avg_num);

  for (uint8_t i = 5; i < (avg_num - 5); i++) {
    vol_sum += Get_ADC_Buff[i];
		if(Get_ADC_Buff[i] == 0)
		{
				null_num++;
				continue;
		}
  }
	/*average */
  vol_res = vol_sum / (avg_num - null_num - 10);
	
	//printf("vol_resualt = %0.3f\n", vol_resualt);
	return ((float)vol_res / (float)4096 * (float)3.3);
}
/********************************************** (C) Copyright MindMotion **********************************************/

